package com.cssw.fyzb.busi.service.system;

import com.alibaba.fastjson.JSON;
import com.cssw.fyzb.core.model.vo.system.ParseWordVo;
import com.cssw.fyzb.core.dao.live.LiveDao;
import com.cssw.fyzb.core.dao.video.UserVideoDao;
import com.cssw.fyzb.core.dao.word.WordCommandDao;
import com.cssw.fyzb.core.mao.user.UserMao;
import com.cssw.fyzb.core.mao.video.VideoStatMao;
import com.cssw.fyzb.core.model.dodb.CmfLive;
import com.cssw.fyzb.core.model.dodb.CmfUserVideo;
import com.cssw.fyzb.core.model.dodb.WordCommand;
import com.cssw.fyzb.core.model.mos.UserMo;
import com.cssw.fyzb.core.util.CodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author YangYiQun
 */
@Service
public class WordCmdServiceImpl implements WordCmdService {

    @Autowired
    private WordCommandDao wordCommandDao;

    @Autowired
    private UserVideoDao videoDao;

    @Autowired
    private VideoStatMao videoStatMao;

    @Autowired
    private LiveDao liveDao;

    @Autowired
    private UserMao userMao;

    @Override
    public String shareVideoWord(Long userId, Long videoId, String type) {
        videoStatMao.incShares(videoId);

        Map<String, Object> param = new HashMap<>();
        param.put("videoId", videoId);
        param.put("type", type);

        Long commandId = wordCommandDao.save(userId, videoId, WordCommand.TYPE_VIDEO, JSON.toJSONString(param));

        //关键字
        String code = CodeUtil.encode(commandId);
        //  CmfUserVideo userVideo = videoDao.findById(videoId,toString());

        StringBuilder content = new StringBuilder();
        //  content.append(userVideo.getTitle()).append("$$").append("v").append(code).append("$$").append("打开福音搜索");

        return content.toString();
    }


    @Override
    public String shareLiveWord(Long userId, Long liveId, String type) {

        Map<String, Object> param = new HashMap<>();
        param.put("liveId", liveId);
        param.put("type", type);

        Long commandId = wordCommandDao.save(userId, liveId, WordCommand.TYPE_LIVE, JSON.toJSONString(param));

        //关键字
        String code = CodeUtil.encode(commandId);
        CmfLive live = liveDao.findOne(liveId);

        StringBuilder content = new StringBuilder();
        content.append(live.getTitle()).append("$$").append("l").append(code).append("$$").append("打开福音搜索");

        return content.toString();
    }

    @Override
    public ParseWordVo parseWordCmd(Long userId, String content) {
        ParseWordVo parseWordVo = new ParseWordVo();
        String[] contentArray = content.split("\\$\\$");
        if (contentArray.length < 3) {
            return null;
        }
        String word = contentArray[contentArray.length - 2];
        try {
            if (word == null || word.length() < 2) {
                return null;
            }
            word = word.substring(1);
            long commandId = Long.valueOf(CodeUtil.decode(word));
            WordCommand wordCommand = wordCommandDao.findById(commandId);
            if (null == wordCommand) {
                return null;
            }

            parseWordVo.setType(wordCommand.getType());

            Long authorUserId = null;
//            if (WordCommand.TYPE_VIDEO.equals(wordCommand.getType())){
//                CmfUserVideo userVideo = videoDao.findById(wordCommand.getSourceId());
//                if (null == userVideo){
//                    return null;
//                }
//                parseWordVo.setVideoId(wordCommand.getSourceId());
//                parseWordVo.setThumb(userVideo.getThum());
//                authorUserId = userVideo.getUserId();
//            } else {
//                CmfLive live = liveDao.findOne(wordCommand.getSourceId());
//                if (null == live){
//                    return null;
//                }
//                authorUserId = live.getUserId();
//                CmfLive living = liveDao.getLivingByUserId(live.getUserId());
//                if (null != living){
//                    parseWordVo.setThumb(living.getThumb());
//                    parseWordVo.setLiveId(living.getLiveId());
//                    parseWordVo.setLiveStatus(living.getStatus());
//                } else{
//                    parseWordVo.setThumb(live.getThumb());
//                    parseWordVo.setLiveStatus(live.getStatus());
//                }
//            }
//
//            UserMo authorUser = userMao.findByUserId(authorUserId);
//            if (null == authorUser){
//                return null;
//            }
//            UserMo shareUser = userMao.findByUserId(wordCommand.getUserId());
//            if (null == shareUser){
//                return null;
//            }
//            parseWordVo.setUserId(authorUserId);
//            parseWordVo.setNickName(authorUser.getNickName());
//
//            parseWordVo.setShareAvatar(shareUser.getAvatar());
//            parseWordVo.setShareNickName(shareUser.getNickName());
//            parseWordVo.setShareUserId(shareUser.getUserId());
//
//            return parseWordVo;
            return null;
        } catch (Exception e) {
            return null;
        }
    }
}
